package indi.zhifa.study2024.common.auth.token;

import com.alibaba.fastjson2.JSON;
import indi.zhifa.core.auth.interfaces.IAuthObject;
import indi.zhifa.core.auth.interfaces.ITokenObject;
import indi.zhifa.core.auth.interfaces.ITokenObjectParser;
import indi.zhifa.core.common.web.entity.exception.ServiceException;

import java.io.InputStream;
import java.util.Base64;

public class TokenObjectParser implements ITokenObjectParser {
    @Override
    public ITokenObject parseFrom(InputStream input) {
        ITokenObject tokenObject;
        try {
            tokenObject = TokenObjectOuterClass.TokenObject.parseFrom(input);
        }catch (Exception e){
            throw new ServiceException("解析TokenObject时失败");
        }
        return tokenObject;
    }

    @Override
    public IAuthObject parseFromDecodedBase64(String pDecodedBase64) {
        byte[] authByte = Base64.getDecoder().decode(pDecodedBase64);
        AuthObject authObject = JSON.parseObject(authByte,AuthObject.class);
        return authObject;
    }
}
